﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using SobFS.IDAL;
using SobFS.Common.Database;

namespace SobFS.MySQLDAL
{
	/// <summary>
	/// 数据访问类:SysRegion
	/// </summary>
	public partial class SysRegion:ISysRegion
	{
		public SysRegion()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("Id", "SysRegion"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SysRegion");
			strSql.Append(" where Id=@Id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
			parameters[0].Value = Id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(SobFS.Model.SysRegion model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SysRegion(");
			strSql.Append("ParentId,Code,Name,RegionType,IsObsoleted,IsEnabled,Description)");
			strSql.Append(" values (");
			strSql.Append("@ParentId,@Code,@Name,@RegionType,@IsObsoleted,@IsEnabled,@Description)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@Code", MySqlDbType.VarChar,50),
					new MySqlParameter("@Name", MySqlDbType.VarChar,50),
					new MySqlParameter("@RegionType", MySqlDbType.Int32,11),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text)};
			parameters[0].Value = model.ParentId;
			parameters[1].Value = model.Code;
			parameters[2].Value = model.Name;
			parameters[3].Value = model.RegionType;
			parameters[4].Value = model.IsObsoleted;
			parameters[5].Value = model.IsEnabled;
			parameters[6].Value = model.Description;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SobFS.Model.SysRegion model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SysRegion set ");
			strSql.Append("ParentId=@ParentId,");
			strSql.Append("Code=@Code,");
			strSql.Append("Name=@Name,");
			strSql.Append("RegionType=@RegionType,");
			strSql.Append("IsObsoleted=@IsObsoleted,");
			strSql.Append("IsEnabled=@IsEnabled,");
			strSql.Append("Description=@Description");
			strSql.Append(" where Id=@Id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@Code", MySqlDbType.VarChar,50),
					new MySqlParameter("@Name", MySqlDbType.VarChar,50),
					new MySqlParameter("@RegionType", MySqlDbType.Int32,11),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text),
					new MySqlParameter("@Id", MySqlDbType.Int32,11)};
			parameters[0].Value = model.ParentId;
			parameters[1].Value = model.Code;
			parameters[2].Value = model.Name;
			parameters[3].Value = model.RegionType;
			parameters[4].Value = model.IsObsoleted;
			parameters[5].Value = model.IsEnabled;
			parameters[6].Value = model.Description;
			parameters[7].Value = model.Id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SysRegion ");
			strSql.Append(" where Id=@Id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
			parameters[0].Value = Id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SysRegion ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SobFS.Model.SysRegion GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,ParentId,Code,Name,RegionType,IsObsoleted,IsEnabled,Description from SysRegion ");
			strSql.Append(" where Id=@Id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
			parameters[0].Value = Id;

			SobFS.Model.SysRegion model=new SobFS.Model.SysRegion();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ParentId"]!=null && ds.Tables[0].Rows[0]["ParentId"].ToString()!="")
				{
					model.ParentId=int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Code"]!=null && ds.Tables[0].Rows[0]["Code"].ToString()!="")
				{
					model.Code=ds.Tables[0].Rows[0]["Code"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RegionType"]!=null && ds.Tables[0].Rows[0]["RegionType"].ToString()!="")
				{
					model.RegionType=int.Parse(ds.Tables[0].Rows[0]["RegionType"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsObsoleted"]!=null && ds.Tables[0].Rows[0]["IsObsoleted"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsObsoleted"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsObsoleted"].ToString().ToLower()=="true"))
					{
						model.IsObsoleted=true;
					}
					else
					{
						model.IsObsoleted=false;
					}
				}
				if(ds.Tables[0].Rows[0]["IsEnabled"]!=null && ds.Tables[0].Rows[0]["IsEnabled"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsEnabled"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsEnabled"].ToString().ToLower()=="true"))
					{
						model.IsEnabled=true;
					}
					else
					{
						model.IsEnabled=false;
					}
				}
				if(ds.Tables[0].Rows[0]["Description"]!=null && ds.Tables[0].Rows[0]["Description"].ToString()!="")
				{
					model.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,ParentId,Code,Name,RegionType,IsObsoleted,IsEnabled,Description ");
			strSql.Append(" FROM SysRegion ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SysRegion ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperMySQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from SysRegion T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "SysRegion";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

